const Base = require('./base.js');

module.exports = class extends Base {
 constructor(ctx) {
		super(ctx);
		this.brand = this.model('store/brand');
		
	}
	async indexAction() {
		if(this.isGet) {
			return this.display();
		}

		let posts = this.post();

		let where = {};
		if(!think.isEmpty(posts.userid)) {
			where.user_id = posts.userid;
		}
		const data = await this.brand.dataTable(posts, where);
		console.log(JSON.stringify(data))
		return this.json(data);
	}
	
	async addAction() {
		if(this.isGet) {
		
			
			return this.display();

		}

		let posts = this.post();
console.log(posts);
		let brand_id = await this.brand.add({
			name: posts.name,
			pic_url: posts.pic_url,
			digest: posts.digest,
			is_show:posts.is_show ? 1:0,
			is_new:posts.is_new ? 1:0
		});

		if(brand_id == 0) {
			return this.fail("添加失败");
		}
		return this.success("添加成功");

	}

	async editAction() {
		if(this.isGet) {
			let id = this.get("id");

			if(id) {
				let brand = await this.brand.findOne(id);

				

				this.assign("brand", brand);
				return this.display();
			}

		}

		let posts = this.post();

		let rows = await this.brand.setOne(posts.id, {
			name: posts.name,
			pic_url: posts.pic_url,
			digest: posts.digest,
			is_show:posts.is_show ? 1:0,
			is_new:posts.is_new ? 1:0
		});

		if(rows == 0) {
			return this.fail("修改失败");
		}
		return this.success("修改成功");
	}
	
	selectAction(){
		if(this.isGet){
			return this.display();
		}
	}


};
